using System;
using System.Collections.Generic;
using System.Data;
using Csla;
using Ent.AccesoDatos;

namespace Ent.Inscripcion.Core.Core
{
    [Serializable]
    public class CatalogoLista :
      ReadOnlyListBase<CatalogoLista, CatalogoInfo>
    {
        

        #region Factory Methods

        public static CatalogoLista GetCatalogoLista(CatalogoCriterio criteria)
        {
            return DataPortal.Fetch<CatalogoLista>(criteria);
        }

        private CatalogoLista()
        { /* require use of factory methods */ }

        #endregion


        #region Data Access

        private void DataPortal_Fetch(CatalogoCriterio criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;
            // TODO: load values
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            var parametros = new List<Parametro>
                                 {
                                     new Parametro("Codigo", criteria.Codigo),
                                     new Parametro("Nombre", criteria.Nombre)
                                 };
            //object objectData = null;
            var datos = accesoDatos.EjecutarReader("ObtenerCatalogos", parametros);
            while (datos.Datos.Read())
            {
                Add(CatalogoInfo.GetCodigoCatalogo(datos));
            }
            if (!datos.Datos.IsClosed) datos.Datos.Close();
            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }


    [Serializable]
    public class CatalogoInfo : ReadOnlyBase<CatalogoInfo>
    {
        #region Business Methods

        private static PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(o => o.Id, "Name");
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
        }

        public static readonly PropertyInfo<string> CodigoProperty = RegisterProperty<string>(c => c.Codigo);
        /// <Summary>
        /// Gets or sets the codigo value.
        /// </Summary>
        public string Codigo
        {
            get { return GetProperty(CodigoProperty); }
        }

        public static readonly PropertyInfo<string> NombreProperty = RegisterProperty<string>(c => c.Nombre);
        // <Summary>
        // Gets or sets the nombre value.
        // </Summary>
        public string Nombre
        {
            get { return GetProperty(NombreProperty); }
        }

        public static readonly PropertyInfo<string> DescripcionProperty = RegisterProperty<string>(c => c.Descripcion);
        /// <Summary>
        /// Gets or sets the Descripcion value.
        /// </Summary>
        public string Descripcion
        {
            get { return GetProperty(DescripcionProperty); }
        }

        #endregion

        #region Factory Methods

        internal static CatalogoInfo GetCodigoCatalogo(Reader childData)
        {
            return DataPortal.FetchChild<CatalogoInfo>(childData);
        }

        private CatalogoInfo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(Reader childData)
        {
            LoadProperty(IdProperty, Guid.Parse(childData.Datos.GetString(0)));
            LoadProperty(CodigoProperty, childData.Datos.GetString(1));
            LoadProperty(NombreProperty, childData.Datos.GetString(2));
            LoadProperty(DescripcionProperty, childData.Datos.GetString(3));
        }

        #endregion
    }
}
